﻿// <copyright file="Program.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the main program of Management</summary>

namespace Sqwarea.Management
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using Lokad.Cloud.Storage;
    using Sqwarea.GameLogic;
    using Sqwarea.GameLogic.Connexity;
    using Sqwarea.GameLogic.Extensions;
    using Sqwarea.Persistence;

    /// <summary>
    /// Program for management
    /// Always empty in SVN - complete only when you need to do a modification on the Cloud
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// User Provider
        /// </summary>
        private static readonly IUserProvider UserProvider;

        /// <summary>
        /// Game Provider
        /// </summary>
        private static readonly IGameProvider GameProvider;

        /// <summary>
        /// Queue Provider
        /// </summary>
        private static readonly IQueueProvider QueueProvider;

        /// <summary>
        /// Global Variable Provider
        /// </summary>
        private static readonly IGlobalVariables GlobalVariables;

        /// <summary>
        /// Initializes static members of the Program class.
        /// More precisely initilizes the providers.
        /// </summary>
        static Program()
        {
            UserProvider = GlobalSetup.Container.Resolve<IUserProvider>();
            GameProvider = GlobalSetup.Container.Resolve<IGameProvider>();
            QueueProvider = GlobalSetup.Container.Resolve<IQueueProvider>();
            GlobalVariables = GlobalSetup.Container.Resolve<IGlobalVariables>();
        }

        /// <summary>
        /// Main function
        /// </summary>
        /// <param name="args">Command lines arguments</param>
        public static void Main(string[] args)
        {
            ////connexityProvider.ConnexityTotalChecker();

////            var queueStorageProvider = GlobalSetup.Container.Resolve<IQueueStorageProvider>();

////            var breakpoints = queueStorageProvider.Get<ConnexityBreakpoint>(
////                Sqwarea.Persistence.Azure.QueueProvider.ConnexityBreakPointQueueName, 20).ToList();
            // WARNING : THIS LINE SHOULD NOT BE REMOVED
////            queueStorageProvider.AbandonRange<ConnexityBreakpoint>(breakpoints);

            // Breakpoints est la liste des 20 derniers breakpoints recensé
            // Remarque : ils ont été remis dans la queue

            // Tu peux appeler ici la connexité mais essaye d'abord de l'appeler sans modification des rois ni des squares
            // (supprime bien le UpdateKing et le buffer commit)
            // Il ne faut absolument pas casser le storage...
            
            /*
            var kings = GameProvider.GetAllKings();
            var userStarter = GlobalSetup.Container.Resolve<IUserStarter>();

            List<User> users = new List<User>();

            foreach (var king in kings)
            {
                if (king.OpenId == null) continue;
                var square = GameProvider.GetSquareByPosition(king.Position);
                if (square.KingId == King.NeutralKingId || king.Position == new Point(15, 0))
                {
                    king.OpenId = null;
                    GameProvider.UpdateKing(king);
                }
            }
             */

            /*
            var a = GlobalVariables.NextKingId;

            var users = new Dictionary<long, User>();
            var userStarter = GlobalSetup.Container.Resolve<IUserStarter>();
            var doubles = new List<User>();

            var blobProvider = GlobalSetup.Container.Resolve<IBlobStorageProvider>();
            var usersContainer = Sqwarea.Persistence.Azure.UserProvider.UserBlobContainerName;
            var openids =
                blobProvider.List(usersContainer, "");

            foreach(var openid in openids)
            {
                var user = blobProvider.GetBlob<User>(usersContainer, openid);

                if(users.ContainsKey(user.Value.Id))
                {
                    doubles.Add(users[user.Value.Id]);
                    doubles.Add(user.Value);
                }
                else
                {
                    users.Add(user.Value.Id, user.Value);
                }
            }

            var doubles2 = doubles.Where(u => u.Id != 0);
            var kingsId = doubles.Select(u => u.Id).Distinct();

            foreach (var user in doubles2)
            {
                var king = userStarter.CreateNewUser(user);
                king.AcademyTroops = 10000;
                king.UpdateAcademyTroops();
                GameProvider.UpdateKing(king);
            }

            foreach (var id in kingsId)
            {
                if(id==0) continue;
                var king = GameProvider.GetKingById(id);
                king.OpenId = null;
                GameProvider.UpdateKing(king);

                var square = GameProvider.GetSquareByPosition(king.Position);
                square.HasKing = false;
                GameProvider.UpdateSquare(square);
            }
             */

            /*
            var connexityProvider = new ConnexityProvider(GameProvider, QueueProvider);
            connexityProvider.ConnexityTotalChecker();
             */

            /*
            var blobProvider = GlobalSetup.Container.Resolve<IBlobStorageProvider>();
            var usersContainer = Sqwarea.Persistence.Azure.UserProvider.UserBlobContainerName;
            var openids =
                blobProvider.List(usersContainer, "");

            foreach (var openid in openids)
            {
                var user = blobProvider.GetBlob<User>(usersContainer, openid).Value;
                if(user.Id == 0) continue;
                var king = GameProvider.GetKingById(user.Id);
                if(king.OpenId==null)
                {
                    king.OpenId = user.OpenId;
                    var square = GameProvider.GetSquareByPosition(king.Position);
                    square.HasKing = true;
                    GameProvider.UpdateKing(king);
                    GameProvider.UpdateSquare(square);   
                }
            }
             */

            /*
            var kings = GameProvider.GetAllKings();

            var bugKings = (from king in kings
                            where king.OpenId != null
                            let user = UserProvider.GetUserByOpenId(king.OpenId)
                            where user.OpenId != king.OpenId
                            select king).ToList();
             */

            return;
        }
    }
}
